/**
 * 
 */
package org.chungyan5.multiplenestingtagging.mobile;

import org.chungyan5.multiplenestingtagging.DbConn;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * @author yan
 *
 */
public class HandleAttachedTagsSimpleCursorAdapter extends SimpleCursorAdapter {

	private Context context;
	private Cursor currentCursor;
	private long itemPointer;
	private String[] from;
	private int[] to;
	
	/**
	 * @param context
	 * @param layout
	 * @param c
	 * @param from
	 * @param to
	 */
	public HandleAttachedTagsSimpleCursorAdapter(Context context, int layout,
			Cursor c, String[] from, int[] to, long itemPointer) {
		super(context, layout, c, from, to);
		this.context = context;
		this.currentCursor = c;
		this.from = from;
		this.to = to;
		this.itemPointer = itemPointer;
	}

	/* (non-Javadoc)
	 * @see android.widget.CursorAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		// prepare a View
		View v = convertView;
		if (v == null) {
			LayoutInflater inflater = 
				(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.single_tag, null);
		}
		
		// prepare a CheckBox and give a name to it
		this.currentCursor.moveToPosition(position);
		CheckBox cBox = (CheckBox) v.findViewById(R.id.bcheck);
		cBox.setTag( Long.parseLong( 
				this.currentCursor.getString(
						this.currentCursor.getColumnIndex(DbConn.TABLE_TAG_COL_TAG_PTR)) 
						) );
		
		if (currentCursor.getLong(currentCursor.getColumnIndex(DbConn.TABLE_TAGS_ITEMS_COL_ITEM_PTRS))==itemPointer)
			cBox.setChecked(true);
		else cBox.setChecked(false);
		
		cBox.setOnClickListener(new OnClickListener() {

    		@Override
    		public void onClick(View v) {
    			CheckBox cBox = (CheckBox) v;
    			
    			if (cBox.isChecked()) {
        			Long tagPointer = (Long) cBox.getTag();
        			
        			ContentValues values = new ContentValues();
        			values.put(DbConn.TABLE_TAGS_ITEMS_COL_ITEM_PTRS, itemPointer);
        			values.put(DbConn.TABLE_TAGS_ITEMS_COL_TAG_PTRS, tagPointer);
        			Uri uri = Uri.withAppendedPath(MNTContentProvider.CONTENT_URI, MNTContentProvider.ITEM_ATTACHED_TAGS_PATH);
        			uri = context.getContentResolver().insert(uri, values);
    			} else {
        			Uri uri = Uri.withAppendedPath(MNTContentProvider.CONTENT_URI, MNTContentProvider.ITEM_ATTACHED_TAGS_PATH);
        			context.getContentResolver().delete(uri, String.valueOf(itemPointer) + "," + String.valueOf((Long) cBox.getTag()), null);
    			}
    			
    		}
        });
		
		// TextView for Tags name 
		TextView listTagNames = (TextView) v.findViewById(to[0]);
		listTagNames.setText(currentCursor.getString(currentCursor.getColumnIndex(from[0])));
		
//		return super.getView(position, convertView, parent);
		return v;
	}

}
